﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LYMesApi.Util;
using System.Configuration;
using System.Data;
using LYMesApi.Models;
using Newtonsoft.Json;
using System.Collections;
using DatabaseAccess.DBUtility;

namespace LYMesApi.Service
{
    /// <summary>
    /// 热表批次入库处理服务
    /// </summary>
    public class RbContainerService
    {
        public string rbFactoryID = ConfigurationManager.AppSettings["RBFactoryID"].ToString();//热表分厂ID
        private DCDataModel model = new DCDataModel();

        public RbContainerService(DCDataModel _model)
        {
            model = _model;
        }

        /// <summary>
        /// 数据处理（唯一对外方法）
        /// </summary>
        public void Excute()
        {
            switch (model.TableName.ToLower())
            {
                case "containercurrentinfo": //处理批次
                    ExcuteContainer();
                    break;
                case "checkqtychangerecord": //处理检验数量变更
                    ExcuteCheckChange();
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 热表批次统一处理方法
        /// </summary>
        /// <param name="model"></param>
        private void ExcuteContainer()
        {
            foreach (Dictionary<string, string> item in model.Items)
            {
                if (item.ContainsKey("isinstore".ToUpper()) && item["isinstore".ToUpper()] == "1") //如果批次的入库字段是1，则表示入库
                {
                    ContainerInstore(item["containerid".ToUpper()]);//处理批次入库
                }
                //后续可以做其他操作
            }
        }

        /// <summary>
        /// 热表检验报废统一处理方法（只需处理批次的子序号即可）
        /// </summary>
        /// <param name="model"></param>
        private void ExcuteCheckChange()
        {
            ArrayList sqlList = new ArrayList();
            foreach (Dictionary<string, string> item in model.Items)
            {
                sqlList.Add(string.Format("UPDATE container c SET c.status = 2,c.qty = 0 WHERE c.containerid = '{0}' AND c.parentcontainerid IS NOT NULL", item["containerid".ToUpper()]));
            }
            DatabaseHelper.ExecuteSqlTran(sqlList);
        }

        /// <summary>
        /// 批次入库
        /// </summary>
        /// <param name="containerID"></param>
        private void ContainerInstore(string containerID)
        {
            //string param = "{ action: \"SaveContainerInstore\",containerid:\""+containerID+"\" }";
            Dictionary<string, string> param = new Dictionary<string, string>();
            param["action"] = "SaveContainerInstore";
            param["containerid"] = containerID;
            //string str = string.Format("{ action: \"SaveContainerInstore\",containerid:\"{0}\" }", containerID);
            string res = HttpUtils.Post("http://10.15.7.75/InSiteWebApplication/Ashx/ReBiaoConainer/RbContainerInstoreManageHandler.ashx", JsonConvert.SerializeObject(param));
            uMESProcessResult result = JsonConvert.DeserializeObject<uMESProcessResult>(res);
            LogHelp.WriteLog(result.Message);
        }


    }
}